<template>
    <div class="container">
        <!-- 路径导航区域 -->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 搜索区域 -->
        <el-row>
            <el-form :inline="true">
                <el-form-item label="用户昵称">
                    <el-input v-model="queryInfo.nickName" clearable @clear="queryList"
                        placeholder="请输入用户昵称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" @click="queryList">搜索</el-button>
                    <el-button type="success" icon="el-icon-plus" @click="toAddUser">新增用户</el-button>
                </el-form-item>
            </el-form>
        </el-row>
        <!-- 表格区域 -->
        <el-table :data="userList" border style="width: 100%" :header-cell-style="{'text-align':'center'}"
            :cell-style="{'text-align':'center'}">
            <el-table-column width="80px" prop="id" label="ID"></el-table-column>
            <el-table-column prop="nickName" label="用户昵称">
                <template slot-scope="scope">
                    <el-tag type="">{{ scope.row.nickName }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="username" label="用户名">
            </el-table-column>
            <el-table-column prop="email" label="邮箱">
            </el-table-column>
            <el-table-column prop="phone" label="联系方式">
            </el-table-column>
            <el-table-column prop="point" label="积分">
                <template slot-scope="scope">
                    <el-tag type="warning">{{ scope.row.point }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="" label="收藏课程">
                <template slot-scope="scope">
                    {{ courseNameList[scope.row.id] }}
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope='scope'>
                    <el-link icon="el-icon-edit" type="primary" @click="toEditUser(scope.row)">重置密码</el-link>
                    <el-link icon="el-icon-delete" type="danger" @click="deleteUser(scope.row.id)">删除用户</el-link>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页区域 -->
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="queryInfo.pageNum" :page-sizes="[1, 5, 10, 15, 20]" :page-size="queryInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>

        <!-- 新增和修改的对话框 -->
        <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancle">
            <el-form :model="user">
                <el-form-item label="用户名" label-width="80px">
                    <el-input placeholder="请输入用户名" v-model="user.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" label-width="80px">
                    <el-input placeholder="请输入密码" v-model="user.password"></el-input>
                </el-form-item>
                <el-form-item label="昵称" label-width="80px">
                    <el-input placeholder="请输入昵称" v-model="user.nickName"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" label-width="80px">
                    <el-input placeholder="请输入邮箱" v-model="user.email"></el-input>
                </el-form-item>
                <el-form-item label="联系方式" label-width="80px">
                    <el-input placeholder="请输入联系方式" v-model="user.phone"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancle">取 消</el-button>
                <el-button type="primary" @click="addUser">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
export default {
    data() {
        return {
            // 查询信息对象
            queryInfo: {
                nickName: '',
                pageNum: 1,
                pageSize: 10
            },
            total: 0,
            userList: [],
            courseNameList: [],
            title: '', // 对话框标题
            dialogFormVisible: false, // 是否显示对话框
            user: { // 对象
                "id": '',
                "username": "",
                "password": "",
                "nickName": "",
                "email": "",
                "phone": "",
                "point": 100
            },
        }
    },
    methods: {
        userCourse() {
            for (let i = 0; i < this.userList.length; i++){
                if (this.userList[i].courses) {
                    let a = [];
                    for (let j = 0; j < this.userList[i].courses.length; j++){
                        // console.log(this.userList[i].courses[j].courseName)
                        a.push(this.userList[i].courses[j].courseName)
                    }
                    this.$set(this.courseNameList, this.userList[i].id, a.toString());
                }
            }
            // console.log(this.courseNameList)
        },
        // 删除
        deleteUser(id) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    this.$http.delete('user/' + id)
                        .then(resp => {
                            if (resp.data.status == 200) {
                                this.$message.success(resp.data.message)
                                this.queryList()
                            } else {
                                this.$message.error(resp.data.message)
                            }
                        })
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    })
                })
        },
        // 进入添加的对话框
        toAddUser() {
            this.title = "新增用户";
            this.dialogFormVisible = true;
        },
        // 重置密码
        toEditUser(row) {
            // 赋值
            this.user = row;
            this.user.password = 123456;
            this.$confirm('将用户密码重置为[123456], 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    this.$http.put("user/edit", this.user)
                        .then(resp => {
                            if (resp.data.status == 200) {
                                // 消息提示
                                this.$message.success('重置成功！');
                                // 清空数据
                                this.user = { // 对象
                                    "id": '',
                                    "username": "",
                                    "password": "",
                                    "nickName": "",
                                    "email": "",
                                    "phone": "",
                                    "point": 100
                                }
                                this.queryList()
                            } else {
                                this.$message.error('接口异常！')
                            }
                        })
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消重置',
                    })
                    // 清空数据
                    this.user = { // 对象
                        "id": '',
                        "username": "",
                        "password": "",
                        "nickName": "",
                        "email": "",
                        "phone": "",
                        "point": 100
                    }
                })
        },
        // 添加
        addUser() {
            this.$http.post("user/add", this.user)
                .then(resp => {
                    if (resp.data.status == 200) {
                        // 消息提示
                        this.$message.success('添加成功！');
                        // 清空数据
                        this.user = { // 对象
                            "id": '',
                            "username": "",
                            "password": "",
                            "nickName": "",
                            "email": "",
                            "phone": "",
                            "point": 100
                        }
                        // 关闭对话框
                        this.dialogFormVisible = false;
                        // 重新查询
                        this.queryList()
                    } else {
                        this.$message.error('接口异常！')
                    }
                })
        },
        cancle() {
            this.dialogFormVisible = false;
            this.user = {
                "id": '',
                "username": "",
                "password": "",
                "nickName": "",
                "email": "",
                "phone": "",
                "point": 100
            }
        },
        queryList() {
            this.$http.get('user/findByPage', {
                params: this.queryInfo
            }).then(res => {
                if (res.data.status == 200) {
                    this.userList = res.data.data.list;
                    this.total = res.data.data.total;
                    this.userCourse()
                } else {
                    this.$message.error('查询失败!')
                }
            })
        },
        handleSizeChange(size) {
            // console.log('每页显示数量发生改变');
            this.queryInfo.pageSize = size
            this.queryInfo.pageNum = 1
            this.queryList()
        },
        handleCurrentChange(num) {
            // console.log('页码发生改变');
            this.queryInfo.pageNum = num
            this.queryList()
        }
    },
    mounted() {
        this.queryList();
    }
}
</script>
<style>
.el-breadcrumb {
    margin-bottom: 15px;
    padding: 0 0 15px;
    border-bottom: 1px solid #ddd;
}

.el-pagination {
    margin-top: 15px;
}

.el-link {
    padding: 0 5px;
}
</style>